<template>
  <div>

    <!-- table -->
    <div style="background: #fff;">
      <div class="  e-p-20  e-flex e-row-between">
        <div class="e-flex">

          <div style="width: 300px;margin-right: 20px;">
            <el-input v-model="initial.params.keyword" placeholder="搜索客户手机号/姓名"></el-input>
          </div>
          <div style="width: 300px;margin-right: 20px;">
            <el-input v-model="initial.params.follow" placeholder="搜索跟进关键词"></el-input>
          </div>
          <el-button type="primary" @click="keyword">搜索</el-button>
        </div>
        <el-button type="warning" @click="take">批量申领</el-button>
      </div>
      <el-table :data="list.data" stripe style="width: 100%" v-if="list.data" height="680" @sort-change="sortChange">
        <el-table-column label="" width="50">
          <template #header>
            <el-checkbox v-model="allCheck" @change="CallCheck"></el-checkbox>
          </template>
          <template slot-scope="scop">
            <el-checkbox v-model="scop.row.checked"></el-checkbox>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="id" label="客户编号">
        </el-table-column> -->
        <el-table-column prop="realname" label="客户姓名">
          <template slot-scope="scop">
            <div @click="flow(scop.row)">{{scop.row.realname}}</div>
            </el-rate>
          </template>
        </el-table-column>
        <el-table-column prop="star" label="跟进状态">
          <template slot-scope="scop">
            <el-rate v-model="scop.row.star" disabled text-color="#ff9900">
            </el-rate>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号">
        </el-table-column>
        <el-table-column prop="type" label="项目类型">
        </el-table-column>
        <el-table-column prop="lastFollowTime" label="跟进时间" sortable='custom' width="200">
        </el-table-column>
        <el-table-column prop="createTime" label="录入时间" width="200" sortable='custom'>
        </el-table-column>


        <el-table-column prop="flow" label="流转">
        </el-table-column>
        <el-table-column prop="company" label="公司名称 ">
        </el-table-column>


        <el-table-column prop="staffName" label="跟进人">
        </el-table-column>
        <el-table-column prop="fromWay" label="客户来源 ">
        </el-table-column>

        <el-table-column label="操作" width="200">
          <template slot-scope="scop">
            <div style="display: flex;">
              <el-button size="mini" @click="take" type="primary" icon="el-icon-edit">申领</el-button>
            </div>
          </template>
        </el-table-column>

      </el-table>

      <flow ref="flowChild" @update="update"></flow>
      <div style="display: flex;justify-content: flex-end;padding: 20px 0;">
        <basePagination :size="list.perPage" :page="list.currentPage" :total="list.total" @SizeChange='SizeChange'
          @CurrentChange='CurrentChange' />
      </div>
    </div>
  </div>
</template>

<script>
  import loader from '@/components/base/basePagination/controls.js';
  import {
    Message
  } from 'element-ui';
  import createdUser from '../../components/createdUser.vue';
  import flow from '../../components/flow.vue';
  export default {
    mixins: [loader],
    components: {
      createdUser,
      flow
    },
    data() {
      return {
        initial: {
          geturl: "/user/clientList",
          params: {
            list: 2,
            keyword: "",
            sort: 'create_time desc',
            follow: ''
          }
        },
        list: {},
        loading: true,
        allCheck: false

      }
    },
    created() {
      this.jiazai()
    },

    methods: {

      sortChange(e) {

        if (e.prop == 'createTime') {
          if (e.order == 'descending') {
            this.initial.params.sort = 'create_time desc'
          } else {
            this.initial.params.sort = 'create_time asc'
          }

        }

        if (e.prop == 'lastFollowTime') {
          if (e.order == 'descending') {
            this.initial.params.sort = 'last_follow_time desc'
          } else {
            this.initial.params.sort = 'last_follow_time asc'
          }

        }
        this.update()

      },
      update() {
        this.initial.page = 1;
        this.jiazai()
      },
      flow(item) {
        let id = item.id;
		this.$refs.flowChild.dialogVisible=true;
        this.$refs.flowChild.jiazai(id);
        this.$refs.flowChild.echo(id);
      },
      CallCheck(e) {
        let data = this.list.data;
        data.forEach(item => {
          item.checked = e;
        })
      },
      keyword() {
        this.initial.page = 1;
        this.jiazai()
      },
      jiazai() {
        this.getData(this.initial.params).then(res => {
          let data = res.data;
          data.forEach(item => {
            item.checked = false;
          })
          this.list = res;
          this.loading = false;
        }).catch(err => {

        })
      },
      take() {
        let data = this.list.data;
        let arr = data.filter(item => item.checked == true);
        let id = [];
        if (arr.length) {
          arr.forEach(item => {
            if (item.checked) {
              id.push(item.id);
            }
          })
          this.axios.post("/user/applyClient", {
            id: id.join(',')
          }).then(res => {
            Message.success({
              message: "申领成功",
              onClose: () => {

                this.jiazai()
              }
            })
          }).catch(error => {})
        } else {
          Message.error({
            message: "请选择客户",
          })
        }
      }
    }
  }
</script>

<style scoped>
  .block {
    background: #fff;
    margin-right: 30px;
    border-radius: 2px;
    font-size: 9px;
    width: 100px;
    text-align: center;
    text-align: center;
    padding: 10px;
    color: #333;

  }

  .change {
    background: #0076D3;
    color: #fff;
    transition: all 0.3s;
  }

  .box {
    margin-bottom: 10px;
  }
</style>
